package com.example.web.controller;
import javax.jms.*;

import org.apache.activemq.ActiveMQConnectionFactory;
public class AppConsumer {

    private static final String url = "tcp://127.0.0.1:61616";
    private static final String topicName="topic-test";

    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic(topicName);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        //connection.close();
    }
    
    private void ab() throws JMSException {
    	  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
          Connection connection = connectionFactory.createConnection();
          connection.start();
          Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
          //创建主题目标
          Destination destination = session.createTopic(topicName);
          MessageProducer producer = session.createProducer(destination);
          for (int i = 0; i < 100; i++) {
              TextMessage message = session.createTextMessage("message"+i);
              producer.send(message);
              System.out.println("发送消息:"+message.getText());
          }
          connection.close();
	}
}